<%@page import="org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileItem"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileUploadException"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileItemFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
	String name="";         
 	String provider="";
    String price="";
    String detail="";
    String path="";
    String SQLpath="image/";
%>
<%
 	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 	if (!isMultipart) {} 
 	else {
 		FileItemFactory factory = new DiskFileItemFactory();
   		ServletFileUpload upload = new ServletFileUpload(factory);
   		List items = null;
   		try {
   			items = upload.parseRequest(request);
   		} catch (FileUploadException e) {
   			e.printStackTrace();
   		}		
   	Iterator itr = items.iterator();
   	while (itr.hasNext()) {
   		FileItem item = (FileItem) itr.next();
   		if (item.isFormField())
   		{
   			String tmp = item.getFieldName();
            String value = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");
            if(tmp.equals("name")) name=value;
           	if(tmp.equals("provider")) provider=value;                         
			if(tmp.equals("price")) price=value;                         
            if(tmp.equals("detail")) detail=value;                            
   		} else
   		{
    		try {
    			String itemName = item.getName();
    			path = "image\\"+itemName;
    			SQLpath+=itemName;
    			SQLpath="\""+SQLpath+"\"";
    			File savedFile = new File(path);
    			item.write(savedFile);
    			} catch (Exception e) {
    				e.printStackTrace();
   				}
    		}
   		}
   	}
 	
	Class.forName("com.mysql.jdbc.Driver");
	Connection con = null;
	Statement smt = null;
	ResultSet rst = null;
	String SQL = null;
	int i = 0;
	if (name.length() == 0 || provider.length() == 0 || price.length() == 0 || detail.length() == 0) response.sendRedirect("addproduct.jsp");
	else
	try {
		String username = "root";
		String password = "root";
		String url = "jdbc:mysql://127.0.0.1:3306/laptop?useUnicode=true&characterEncoding=UTF-8";
		con = DriverManager.getConnection(url, username, password);
		System.out.println("Da ket noi CSDL");
		smt = con.createStatement();
		rst = smt.executeQuery("select * from product");
		while (rst.next()) i++;
		System.out.println(i);
		i++;
		SQL = "INSERT INTO product VALUES ('"+i+"', '"+name+"', '"+provider+"', '"+price+"', '"+SQLpath+"', '"+detail+"', 'còn hàng')";
		System.out.println(SQL);
		smt.execute(SQL);
		con.close();
		System.out.println("Dong ket noi");
		SQLpath = null;
		response.sendRedirect("productmanager.jsp");
	}
	catch (Exception e) {
		e.printStackTrace();
		System.out.println("KHONG KET NOI DUOC");
	}

%>
</body>
</html>